'use strict';

const Controller = require('egg').Controller;

class TeacherController extends Controller {
    // 创建控制器——POST——/name
    async create() {
        const { ctx } = this;
        const { name, gender, t_type, telephone } = ctx.request.body;
        const picture = 'http://localhost:8890/public/Teacher_Default_Avatar.jpg';
        const password = 'e10adc3949ba59abbe56e057f20f883e';
        const deleted = 0;
        const teacher = {
            name,
            gender,
            t_type,
            telephone,
            picture,
            password,
            deleted
        }
        const result = await ctx.service.teacher.add(teacher);

        ctx.status = 201;
        ctx.body = result;
    }

    // 查询获取数据用控制器——GET——/name?keywords=?&&.....
    async index() {
        const { ctx } = this;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        console.log('keywords', keywords);
        const result = await ctx.service.teacher.findList(keywords, pageIndex, pageSize);
        ctx.status = 200;
        ctx.body = result;
    }
    // 根据id获取信息的控制器方法——GET——/name/:id
    async show() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.teacher.findById(id);
        ctx.status = 200;
        ctx.body = result;
    }
    // 根据id修改对应的数据的控制器方法——PUT——/name/:id
    async update() {
        const { ctx } = this;
        const id = ctx.params.id;
        const { 
            name,
            gender,
            t_type,
            telephone,
            picture,
        } = ctx.request.body;
        const result = await ctx.service.teacher.modify({ 
            id,
            name,
            gender,
            t_type,
            telephone,
            picture,
        });
        ctx.body = result;
    }
    // // 根据id删除对应的数据的控制器方法——DELETE——/name/:id
    async destroy(){
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.teacher.remove(id);
        ctx.status = 200;
        ctx.body = result;
    }

}

module.exports = TeacherController;
